package client.web;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class IndexController {

    @GetMapping
    public String index() {
        return "Hello OAuth2 World!";
    }

    @ResponseBody
    @GetMapping("/hello")
    public JwtAuthenticationToken hello() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication instanceof JwtAuthenticationToken jwtAuthenticationToken) {
            log.info(STR."登录成功!\n用户名:\{jwtAuthenticationToken.getName()}");
            return jwtAuthenticationToken;
        }
        return null;
    }

}
